package login;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

import palabrasClave.BuscadorPalabrasClave;

public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUsuario;
	private JPasswordField txtPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			LoginDialog dialog = new LoginDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		setBounds(100, 100, 364, 155);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNombreUsuario = new JLabel("Nombre de usuario:");
			lblNombreUsuario.setBounds(10, 13, 168, 14);
			contentPanel.add(lblNombreUsuario);
		}
		{
			txtUsuario = new JTextField();
			txtUsuario.setBounds(203, 10, 114, 20);
			contentPanel.add(txtUsuario);
			txtUsuario.setColumns(10);
		}
		{
			JButton okButton = new JButton("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (Login.authenticate(txtUsuario.getText(),txtPassword.getText()))
					{
							new BuscadorPalabrasClave(txtUsuario.getText());
							dispose();
					}
					
					else
					{
	                    JOptionPane.showMessageDialog(LoginDialog.this,
	                            "Nombre de usuario o password invalidos",
	                            "Login",
	                            JOptionPane.ERROR_MESSAGE);
	                    // reset username and password
	                    txtUsuario.setText("");
	                    txtPassword.setText("");   
	                    txtUsuario.grabFocus();
	                }
				}
			});
			
			
			okButton.setBounds(97, 69, 80, 23);
			contentPanel.add(okButton);
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			cancelButton.setBounds(203, 69, 86, 23);
			contentPanel.add(cancelButton);
			cancelButton.setActionCommand("Cancel");
		}
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(10, 41, 168, 14);
		contentPanel.add(lblPassword);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(203, 41, 114, 19);
		contentPanel.add(txtPassword);
	}
}
